import java.util.Scanner;

public class U_5_2 {

    public void greedyPackage(int capacity, int[] weight, int[] value,int max_weight) {
        int n = weight.length;
        double[] price = new double[n];
        int[] count = new int[n];
        for (int i = 0; i < n; i++) {
            price[i] = (double) value[i] / weight[i];
            count[i] = i;
        }
        for (int i = 0; i < n - 1; i++) {
            for (int j = i; j < n - 1; j++) {
                if (price[j] < price[j + 1]) {
                    double tmp = price[j];
                    price[j] = price[j + 1];
                    price[j + 1] = tmp;
                    int a = count[j];
                    count[j] = count[j + 1];
                    count[j + 1] = a;
                }
            }
        }
        int[] newWeight = new int[n];
        int[] newValue = new int[n];
        for (int i = 0; i < n; i++) {
            newValue[i] = value[count[i]];
            newWeight[i] = weight[count[i]];
        }
        double maxValue = 0;
        for (int i = 0; i < n; i++) {
            if (capacity > newWeight[i]) {
                capacity -= newWeight[i];
                maxValue += newValue[i];
            }
        }
        System.out.print("共放下了" + (max_weight - capacity) + "kg重的东西\n");
        System.out.print("总价值" + maxValue);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int allW = sc.nextInt(),num = sc.nextInt();
        int[] weight = new int[num],value = new int[num];
        for (int i = 0; i < num; i++) {
            weight[i] = sc.nextInt();
            value[i] = sc.nextInt();
        }
        U_5_2 greedyPackage = new U_5_2();
        greedyPackage.greedyPackage(allW, weight, value,allW);
    }
}

